/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterestPurpos.java
 *
 * Created on Oct 17, 2011, 7:41:43 AM
 */
package project;

import Utils.Helper;
import Utils.JDBCHelper;
import Utils.checkValidate;
import beanControl.interestControl;
import beanControl.purposesControl;
import beanInfo.interest;
import beanInfo.purposes;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;

/**
 *
 * @author lyan
 */
public class InterestPurpos extends javax.swing.JFrame {

    interestControl inct = new interestControl();
    interest in = new interest();
    purposes pu = new purposes();
    purposesControl puct = new purposesControl();
    String id="";
    /** Creates new form InterestPurpos */
    public InterestPurpos(String ID) {
        initComponents();
        this.setSize(460, 430);
        this.setLocation(400, 100);
        loadcbbInterestTypeID(cbbTypeID);
        this.id=ID;
        display();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        btnSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        txtLimtit = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtUnmit = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        cbbTypeID = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        lblInterestID = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtYear = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtLoanOfject = new javax.swing.JTextField();
        lblPurposesID = new javax.swing.JLabel();
        btnUpdate = new javax.swing.JButton();

        getContentPane().setLayout(null);

        jPanel2.setBackground(new java.awt.Color(0, 102, 102));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(null);

        btnSave.setBackground(new java.awt.Color(0, 102, 102));
        btnSave.setForeground(new java.awt.Color(255, 255, 255));
        btnSave.setText("Insert");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        jPanel1.add(btnSave);
        btnSave.setBounds(110, 310, 70, 30);

        btnCancel.setBackground(new java.awt.Color(0, 102, 102));
        btnCancel.setForeground(new java.awt.Color(255, 255, 255));
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        jPanel1.add(btnCancel);
        btnCancel.setBounds(200, 310, 80, 30);

        jLabel1.setBackground(new java.awt.Color(255, 255, 255));
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel1.setForeground(new java.awt.Color(204, 0, 0));
        jLabel1.setText("Interest and Purpos");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(150, 30, 250, 30);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Interesr", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Agency FB", 1, 18), new java.awt.Color(255, 0, 0))); // NOI18N
        jPanel3.setLayout(null);
        jPanel3.add(txtLimtit);
        txtLimtit.setBounds(150, 30, 240, 30);

        jLabel6.setText("LimitInterset");
        jPanel3.add(jLabel6);
        jLabel6.setBounds(60, 30, 70, 30);
        jPanel3.add(txtUnmit);
        txtUnmit.setBounds(150, 70, 240, 30);

        jLabel7.setText("UnLimitInterest");
        jPanel3.add(jLabel7);
        jLabel7.setBounds(60, 70, 80, 30);

        cbbTypeID.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel3.add(cbbTypeID);
        cbbTypeID.setBounds(150, 110, 240, 30);

        jLabel8.setText("InterestTypeID");
        jPanel3.add(jLabel8);
        jLabel8.setBounds(50, 120, 90, 20);
        jPanel3.add(lblInterestID);
        lblInterestID.setBounds(70, 20, 40, 10);

        jLabel2.setText("Years");
        jPanel3.add(jLabel2);
        jLabel2.setBounds(70, 150, 50, 30);
        jPanel3.add(txtYear);
        txtYear.setBounds(150, 150, 240, 30);

        jLabel3.setText("objectLoan");
        jPanel3.add(jLabel3);
        jLabel3.setBounds(50, 190, 80, 30);
        jPanel3.add(txtLoanOfject);
        txtLoanOfject.setBounds(150, 190, 240, 30);
        jPanel3.add(lblPurposesID);
        lblPurposesID.setBounds(10, 30, 30, 30);

        jPanel1.add(jPanel3);
        jPanel3.setBounds(10, 70, 420, 230);

        btnUpdate.setBackground(new java.awt.Color(0, 102, 102));
        btnUpdate.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        btnUpdate.setForeground(new java.awt.Color(255, 255, 255));
        btnUpdate.setText("Update");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });
        jPanel1.add(btnUpdate);
        btnUpdate.setBounds(290, 310, 80, 30);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 472, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(18, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(15, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 374, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        getContentPane().add(jPanel2);
        jPanel2.setBounds(0, 0, 500, 400);

        pack();
    }// </editor-fold>//GEN-END:initComponents

     public interest SelectInter() {
       
        try {

            String sql = "select InterestID,InterestTypeID,LimitInterset,UnLimitInterest,a.Years,b.ObjectLoan from Interest a,Purposes b where InterestID='" + id + "' ";
            ResultSet rs = JDBCHelper.excuteSelect(sql);
            while (rs.next()) {
                in.setInterestID(rs.getInt("InterestID"));
                in.setInterestTypeID(rs.getInt("InterestTypeID"));
                in.setLimitInterset(rs.getFloat("LimitInterset"));
                in.setUnLimitInterest(rs.getFloat("UnLimitInterest"));
                //  us.setBirthday(rs.getString("Brithday"));
                in.setYears(rs.getInt("Years"));
               


            }
        } catch (SQLException ex) {
            Logger.getLogger(UserFrom.class.getName()).log(Level.SEVERE, null, ex);
        }
        return in;
    }
     public purposes SelectPorpu()
     {
            try {

            String sql = "select * from Purposes where PurposesID='" + id + "' ";
            ResultSet rs = JDBCHelper.excuteSelect(sql);
            while (rs.next()) {
                in.setPurposesID(rs.getInt("PurposesID"));
                in.setYears(rs.getInt("Years"));
                
                //  us.setBirthday(rs.getString("Brithday"));
                pu.setObjectLoan(rs.getString("ObjectLoan"));
               
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserFrom.class.getName()).log(Level.SEVERE, null, ex);
        }
        return pu;  
     }
      private void display() {

        if (Admin.cruUser == 0) {
            txtLimtit.setText("");
            txtLoanOfject.setText("");
            txtUnmit.setText("");
            txtYear.setText("");
           
        } else {
            interest i = SelectInter();
            purposes p=new purposes();
            txtUnmit.setText(String.valueOf(i.getUnLimitInterest()));
            txtLimtit.setText(String.valueOf(i.getLimitInterset()));
            txtYear.setText(String.valueOf(i.getYears()));
            txtLoanOfject.setText(p.getObjectLoan());
            //   Helper.ConvertDateToString(dbBirthday.setDate(u.getBirthday().toString()));
           
        }
    }
     
     
    public void insertInterest() {
        in.setUpdateDate(Helper.ConvertDateToString(new java.util.Date()));
        in.setLimitInterset(Float.parseFloat(txtLimtit.getText()));
        in.setUnLimitInterest(Float.parseFloat(txtUnmit.getText()));
        in.setInterestTypeID(Integer.parseInt(Helper.splitString(cbbTypeID.getSelectedItem().toString(), "-")));
        in.setYears(Integer.parseInt(txtYear.getText()));
        in.setPurposesID(inct.getMaxPurposesID());
        in.setStatus("false");
        inct.insertInterst(in);
    }

    public void updateInterest() {
        in.setUpdateDate(Helper.ConvertDateToString(new java.util.Date()));
        in.setLimitInterset(Float.parseFloat(txtLimtit.getText()));
        in.setUnLimitInterest(Float.parseFloat(txtUnmit.getText()));
        in.setInterestTypeID(Integer.parseInt(cbbTypeID.getSelectedItem().toString()));
        in.setYears(Integer.parseInt(txtYear.getText()));
        in.setPurposesID(inct.getMaxPurposesID());
        in.setStatus("fasle");
        inct.updateCusroner(in);
    }

    public void insertPurpos() {

        pu.setObjectLoan(txtLoanOfject.getText());
        pu.setYears(Integer.parseInt(txtYear.getText()));
        pu.setStatus("false");
        puct.insertPurpose(pu);
    }

    public void updatePurpos() {

        pu.setObjectLoan(txtLoanOfject.getText());
        pu.setYears(Integer.parseInt(txtYear.getText()));

        pu.setStatus("false");
        puct.updatepurposes(pu);
    }

    public void loadcbbInterestTypeID(JComboBox cbo) {
        String sql = "select InterestTypeID, InterrestName from InterestType";
        Helper.getListCBO(sql, cbo, "InterrestName", "InterestTypeID");

    }

private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
// TODO add your handling code here:
    if (txtLimtit.getText().length() == 0) {
        Helper.successMessage("Enter LimitInterset ", "wanning");

    } else if (txtLoanOfject.getText().length() == 0) {
        Helper.successMessage("Enter OfjectLoan ", "wanning");
    } else if (txtYear.getText().length() == 0) {
        Helper.successMessage("Enter year ", "wanning");
    } else if (txtUnmit.getText().length() == 0) {
        Helper.successMessage("Enter UnLimitInterest ", "wanning");
    } else if (checkValidate.checkNumber(txtYear.getText()) == false) {
        Helper.successMessage("years is number", "wanning");
    } else if (checkValidate.checkFloatNumber(txtLimtit.getText()) == false) {
        Helper.successMessage("LimitInterset is number", "wanning");
    } else if (checkValidate.checkFloatNumber(txtUnmit.getText()) == false) {
        Helper.successMessage("UnLimitInterestis number", "wanning");
    } else {
        insertPurpos();
        insertInterest();

    }

}//GEN-LAST:event_btnSaveActionPerformed

private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
// TODO add your handling code here:
    if (txtLimtit.getText().length() == 0) {
        Helper.successMessage("Enter LimitInterset ", "wanning");

    } else if (txtLoanOfject.getText().length() == 0) {
        Helper.successMessage("Enter OfjectLoan ", "wanning");
    } else if (txtYear.getText().length() == 0) {
        Helper.successMessage("Enter year ", "wanning");
    } else if (txtUnmit.getText().length() == 0) {
        Helper.successMessage("Enter UnLimitInterest ", "wanning");
    } else if (checkValidate.checkNumber(txtYear.getText()) == false) {
        Helper.successMessage("years is number", "wanning");
    } else if (checkValidate.checkFloatNumber(txtLimtit.getText()) == false) {
        Helper.successMessage("LimitInterset is number", "wanning");
    } else if (checkValidate.checkFloatNumber(txtUnmit.getText()) == false) {
        Helper.successMessage("UnLimitInterestis number", "wanning");
    } else {
        updatePurpos();
        updateInterest();
    
    }
}//GEN-LAST:event_btnUpdateActionPerformed

    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(InterestPurpos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(InterestPurpos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(InterestPurpos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(InterestPurpos.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
//        /* Create and display the form */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//
//            public void run() {
//                new InterestPurpos().setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox cbbTypeID;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lblInterestID;
    private javax.swing.JLabel lblPurposesID;
    private javax.swing.JTextField txtLimtit;
    private javax.swing.JTextField txtLoanOfject;
    private javax.swing.JTextField txtUnmit;
    private javax.swing.JTextField txtYear;
    // End of variables declaration//GEN-END:variables
}
